<?php
/**
 * Created by PhpStorm.
 * User: ownfi
 * Date: 2017-04-17
 * Time: 10:38
 */

namespace App\Services\Impl;


use App\Models\Article;
use App\Models\Banner;
use App\Services\IArticleService;

class ArticleServiceImpl implements IArticleService
{
    public function all(array $wheres = [])
    {
        $model = Article::alias('t1')->select('t1.*')->orderBy('t1.add_time','DESC');
        if ($wheres){
            $model->where($wheres);
        }
        return $model->paginate(config('common.perpage'));
    }

    public function add($title, $content, $cat_id)
    {
        return Article::create([
            'cat_id'=>$cat_id,
            'title'=>$title,
            'content'=>$content,
        ]);
    }

    public function edit($article_id, $title, $content, $cat_id)
    {
        return Article::find($article_id)->update([
            'cat_id'=>$cat_id,
            'title'=>$title,
            'content'=>$content,
        ]);
    }

    public function delete($article_id)
    {
        return Article::find($article_id)->delete();
    }

    public function bannerList()
    {
        return Banner::orderBy('sort','DESC')->orderBy('add_time','DESC')->get();
    }

    public function bannerAdd($name, $banner_img, $sort = 0,$url)
    {
        return Banner::create([
            'name'=>$name,
            'banner_img'=>$banner_img,
            'sort'=>$sort,
            'url'=>$url,
        ]);
    }

    public function bannerEdit($banner_id, $name, $banner_img, $sort = 0,$url)
    {
        return Banner::find($banner_id)->update([
            'name'=>$name,
            'banner_img'=>$banner_img,
            'sort'=>$sort,
            'url'=>$url,
        ]);
    }

    public function bannerDelete($banner_id)
    {
        return Banner::find($banner_id)->delete();
    }

}